package com.gopay.bis.cmbc.ebank.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gopay.bis.cmbc.ebank.operation.CmbcParseMessage;
import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmbc.ebank.CmbcMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.HttpsTransport;
import com.gopay.remote.bank.ICertHelper;
import com.gopay.remote.bank.businesses.BankService;

/**
 * 民生银行签名/验签接口实现
 * 
 */
public class CmbcB2cBankServiceImpl implements BankService {

	/** B2C支付交易签名实现类 */
	private ICertHelper cmbcB2cCerthelper;
	/** HTTP实现类 */
	private HttpsTransport httpTransportImpl;
	/** B2C对账交易返回解析类 */
	private CmbcParseMessage cmbcParseMessage;
	
	private static Log logger = LogFactory.getLog(CmbcB2cBankServiceImpl.class);
	/**
	 * CMBC签名处理
	 * 
	 * @throws GopayException
	 */
	public String sign(BaseMessage message) throws GopayException {
		CmbcMessage cmbcMessage = (CmbcMessage) message;
		String signMsg = "";
		signMsg = (String) cmbcB2cCerthelper.sign(cmbcMessage);
		return signMsg;
	}

	/**
	 * CMBC验签处理
	 * 
	 * @throws GopayException
	 */
	public Object verify(BaseMessage msg) throws GopayException {
		CmbcMessage cmbcMessage = (CmbcMessage) msg;
		String verifyResult = "";
		verifyResult = (String) cmbcB2cCerthelper.verify(cmbcMessage);
		return verifyResult;
	}

	/**
	 * CMBC订单查询处理
	 */
	public Object query(Object object) throws GopayException {
		try {
			CmbcMessage cmbcMessage = (CmbcMessage) object;
			CmbcMessage retMessage = null;
			String signNatrue = "";
			signNatrue = (String) cmbcB2cCerthelper.sign(cmbcMessage);
			Map tempMap = new HashMap();
			tempMap.put("cryptograph", signNatrue);
			httpTransportImpl.setUrl(cmbcMessage.getQueryUrl());
			Object obj = (Object) httpTransportImpl.submit(tempMap); // 发送银行
			retMessage = cmbcParseMessage.parseCmbcMessage(obj,cmbcMessage);// 解析银行返回参数
			return retMessage;
		} catch (Exception e) {
			BankLogUtils.printB2CQueryExceptionLog((BaseMessage) object, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6037);
		}

	}

	public ICertHelper getCmbcB2cCerthelper() {
		return cmbcB2cCerthelper;
	}

	public void setCmbcB2cCerthelper(ICertHelper cmbcB2cCerthelper) {
		this.cmbcB2cCerthelper = cmbcB2cCerthelper;
	}

	public HttpsTransport getHttpTransportImpl() {
		return httpTransportImpl;
	}

	public void setHttpTransportImpl(HttpsTransport httpTransportImpl) {
		this.httpTransportImpl = httpTransportImpl;
	}

	public CmbcParseMessage getCmbcParseMessage() {
		return cmbcParseMessage;
	}

	public void setCmbcParseMessage(CmbcParseMessage cmbcParseMessage) {
		this.cmbcParseMessage = cmbcParseMessage;
	}

	@Override
	public Object dayend(Object obj) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(Object obj) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}


}
